/* eslint-disable no-unused-expressions */
/* eslint-disable array-callback-return */
/* eslint-disable no-param-reassign */
import { message } from 'antd';
import {
  getPageList,
  getFetchCode,
  deleteBaseRole,
  updateBaseRole,
} from '@/services/authoritymanage';

export default {
  state: {
    pageData: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    formFieldsValue: {},
    expandForm: false,
    valuesArr: [
      'roleName',
      'sysId',
      'createTime',
    ],
  },
  effects: {
    // 获取列表
    *fetchList({ payload }, { call, put }) {
      const response = yield call(getPageList, payload);
      const {
        page: { currentPage, pageSizes },
      } = payload;
      if (response.success) {
        yield put({
          type: 'getList',
          payload: {
            data: response.data,
            currentPage,
            pageSize: pageSizes,
          },
        });
      } else {
        message.warning(`${response.message || '服务端错误！'}`);
      }
    },
    // 获取所属系统字典
    *fetchDictCode({ payload, callback }, { call }) {
      const response = yield call(getFetchCode, payload);
      callback && callback(response);
    },
    *deleteBaseRole({ payload }, { call }) {
      const response = yield call(deleteBaseRole, payload);
      return response;
    },
    *updateBaseRole({ payload }, { call }) {
      const response = yield call(updateBaseRole, payload);
      return response;
    },
  },

  reducers: {
    // 获取列表
    getList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        pageData: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },
    // 设置修改当前state数据
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    // 设置页签状态
    updateTabListQuery(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
